package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.StrategyCatalog;
import cn.wolfcode.trip.base.mapper.StrategyCatalogMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IStrategyCatalogService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StrategyCatalogServiceImpl implements IStrategyCatalogService {
    @Autowired
    private StrategyCatalogMapper strategyCatalogMapper;

    @Override
    public PageInfo query(QueryObject qo) {
        //排序
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<StrategyCatalog> list = strategyCatalogMapper.selectForList(qo);
        return new PageInfo(list);
    }

    @Override
    public void saveOrUpdate(StrategyCatalog strategyCatalog) {
        if (strategyCatalog.getSequence()==null){
            //序号为null,自动增长
            strategyCatalog.setSequence(
                    //查询所属类别的序号
                    strategyCatalogMapper.selectAutoSequence(strategyCatalog.getStrategy().getId())+1);
        }
        if (strategyCatalog.getId()!=null){
            //修改
            strategyCatalogMapper.updateByPrimaryKey(strategyCatalog);
        }else {
            //保存
            strategyCatalogMapper.insert(strategyCatalog);
        }
    }

    @Override
    public List<StrategyCatalog> getCatalogByStrategyId(Long strategyId) {
        return strategyCatalogMapper.selectCatalogByStrategyId(strategyId);
    }
}
